class TestRagdoll extends Test
{
    public constructor(sprite:DebugDraw)
    {
        super(sprite);
        this.createScene();
    }

    private createScene():void
    {
        
        //add 5 ragdolls along the top;
        for(var i:number = 0; i < 1; i++)
        {
            this.createDoll(i);
        }

        //add staris
    }

    private createDoll(i:number):void
    {
        let circ:Box2D.Collision.Shapes.b2CircleShape;
        let box:Box2D.Collision.Shapes.b2PolygonShape;
        let bd:Box2D.Dynamics.b2BodyDef = new Box2D.Dynamics.b2BodyDef();
        let jd:Box2D.Dynamics.Joints.b2RevoluteJointDef = new Box2D.Dynamics.Joints.b2RevoluteJointDef();
        let fixtureDef:Box2D.Dynamics.b2FixtureDef = new Box2D.Dynamics.b2FixtureDef();


        let startX:number = 70 + Math.random() * 20 + 480 * i;
        let startY:number = 20 + Math.random() * 50;

        //bodies set thess to dynamic bodies;
        bd.type = Box2D.Dynamics.b2Body.b2_dynamicBody;

        circ = new Box2D.Collision.Shapes.b2CircleShape(12.5 / this.m_physScale);
        fixtureDef.shape = circ;
        fixtureDef.density = 1.0;
        fixtureDef.friction = 0.4;
        fixtureDef.restitution = 0.3;
        bd.position.Set(startX / this.m_physScale, startY / this.m_physScale);
        var head:Box2D.Dynamics.b2Body = this.m_world.CreateBody(bd);
        head.CreateFixture(fixtureDef);

        head.ApplyImpulse(new Box2D.Common.Math.b2Vec2(Math.random() * 100 - 50, Math.random() * 100 - 50),head.GetWorldCenter());

        //torso1
        box = new Box2D.Collision.Shapes.b2PolygonShape();
        box.SetAsBox(15 / this.m_physScale, 10 / this.m_physScale);
        fixtureDef.shape = box;
        fixtureDef.density = 1.0;
        fixtureDef.friction = 0.4;
        fixtureDef.restitution = 0.1;
        bd.position.Set(startX / this.m_physScale, (startY + 28) / this.m_physScale);
        var torso1:Box2D.Dynamics.b2Body = this.m_world.CreateBody(bd);
        torso1.CreateFixture(fixtureDef);
        //torso2
        box = new Box2D.Collision.Shapes.b2PolygonShape();
        box.SetAsBox(15 / this.m_physScale, 10 / this.m_physScale);
        fixtureDef.shape = box;
        bd.position.Set(startX / this.m_physScale, (startY + 43) / this.m_physScale);
        var torso2:Box2D.Dynamics.b2Body = this.m_world.CreateBody(bd);
        torso2.CreateFixture(fixtureDef);

        //torso3
        box = new Box2D.Collision.Shapes.b2PolygonShape();
        box.SetAsBox(15 / this.m_physScale, 10 / this.m_physScale);
        fixtureDef.shape = box;
        bd.position.Set(startX / this.m_physScale, (startY + 58) / this.m_physScale);
        var torso3:Box2D.Dynamics.b2Body = this.m_world.CreateBody(bd);
        torso3.CreateFixture(fixtureDef);

        // upper arm
        fixtureDef.density = 1.0;
        fixtureDef.friction = 0.4;
        fixtureDef.restitution = 0.1;
        //l
        box = new Box2D.Collision.Shapes.b2PolygonShape();
        box.SetAsBox(18 / this.m_physScale, 6.5 / this.m_physScale);
        fixtureDef.shape = box;
        bd.position.Set((startX - 30) / this.m_physScale,
                        (startY + 20) / this.m_physScale);
        let upperArmL:Box2D.Dynamics.b2Body = this.m_world.CreateBody(bd);
        upperArmL.CreateFixture(fixtureDef);
        //r
        box = new Box2D.Collision.Shapes.b2PolygonShape();
        box.SetAsBox(18 / this.m_physScale, 6.5 / this.m_physScale);
        fixtureDef.shape = box;
        bd.position.Set((startX + 30) / this.m_physScale,
                        (startY + 20) / this.m_physScale);
        let upperArmR:Box2D.Dynamics.b2Body = this.m_world.CreateBody(bd);
        upperArmR.CreateFixture(fixtureDef);

        //lower arm
        //l
        box = new Box2D.Collision.Shapes.b2PolygonShape();
        box.SetAsBox(17 / this.m_physScale, 6 / this.m_physScale);
        fixtureDef.shape = box;
        bd.position.Set((startX + 57) / this.m_physScale,
                        (startY + 20) / this.m_physScale);
        let lowerArmL:Box2D.Dynamics.b2Body = this.m_world.CreateBody(bd);
        lowerArmL.CreateFixture(fixtureDef);
        //r
        box = new Box2D.Collision.Shapes.b2PolygonShape();
        box.SetAsBox(17 / this.m_physScale, 6 / this.m_physScale);
        fixtureDef.shape = box;
        bd.position.Set((startX - 57) / this.m_physScale,
                        (startY + 20) / this.m_physScale);
        let lowerArmR:Box2D.Dynamics.b2Body = this.m_world.CreateBody(bd);
        lowerArmR.CreateFixture(fixtureDef);

        //UpperLeg;
        //l
        box = new Box2D.Collision.Shapes.b2PolygonShape();
        box.SetAsBox(7.5 / this.m_physScale, 22 / this.m_physScale);
        fixtureDef.shape = box;
        bd.position.Set((startX - 8) / this.m_physScale,
                        (startY + 85) / this.m_physScale);
        let upperLegL:Box2D.Dynamics.b2Body = this.m_world.CreateBody(bd);
        upperLegL.CreateFixture(fixtureDef);
        //r
        box = new Box2D.Collision.Shapes.b2PolygonShape();
        box.SetAsBox(7.5 / this.m_physScale, 22 / this.m_physScale);
        fixtureDef.shape = box;
        bd.position.Set((startX + 8) / this.m_physScale,
                        (startY + 85) / this.m_physScale);
        let upperLegR:Box2D.Dynamics.b2Body = this.m_world.CreateBody(bd);
        upperLegR.CreateFixture(fixtureDef);

        //LowerLeg;
        //l
        box = new Box2D.Collision.Shapes.b2PolygonShape();
        box.SetAsBox(6 / this.m_physScale, 20 / this.m_physScale);
        fixtureDef.shape = box;
        bd.position.Set((startX - 8) / this.m_physScale,
                        (startY + 120) / this.m_physScale);
        let lowerLegL:Box2D.Dynamics.b2Body = this.m_world.CreateBody(bd);
        lowerLegL.CreateFixture(fixtureDef);
        //r
        box = new Box2D.Collision.Shapes.b2PolygonShape();
        box.SetAsBox(6/ this.m_physScale, 20 / this.m_physScale);
        fixtureDef.shape = box;
        bd.position.Set((startX + 8) / this.m_physScale,
                        (startY + 120) / this.m_physScale);
        let lowerLegR:Box2D.Dynamics.b2Body = this.m_world.CreateBody(bd);
        lowerLegR.CreateFixture(fixtureDef);

        //joins;
        //head shouder
        jd.enableLimit = true;

        jd.lowerAngle = -40 / (180 / Math.PI);
        jd.upperAngle = 40 / (180 / Math.PI);
        jd.Initialize(torso1,head, new Box2D.Common.Math.b2Vec2(startX / this.m_physScale, 
                                                                (startY + 15) / this.m_physScale));
        this.m_world.CreateJoint(jd);
        //uppper arm to shoulder
        //l
        jd.lowerAngle = -85 / (180 / Math.PI);
        jd.upperAngle = 130 / (180 / Math.PI);
        jd.Initialize(torso1,upperArmL, new Box2D.Common.Math.b2Vec2((startX - 18) / this.m_physScale, 
                                                                    (startY + 20) / this.m_physScale));
        this.m_world.CreateJoint(jd);
        //r
        jd.enableLimit = true;
        jd.lowerAngle = -130 / (180 / Math.PI);
        jd.upperAngle = 85 / (180 / Math.PI);
        jd.Initialize(torso1,upperArmL, new Box2D.Common.Math.b2Vec2((startX + 18) / this.m_physScale, 
                                                                    (startY + 20) / this.m_physScale));
        this.m_world.CreateJoint(jd);

        //lower arm to uppeer arm
        //l
        jd.lowerAngle = -130 / (180 / Math.PI);
        jd.upperAngle = 10 / (180 / Math.PI);
        jd.Initialize(upperArmL,lowerArmL, new Box2D.Common.Math.b2Vec2((startX - 45) / this.m_physScale, 
                                                                    (startY + 20) / this.m_physScale));
        this.m_world.CreateJoint(jd);
        //r
        jd.lowerAngle = -10 / (180 / Math.PI);
        jd.upperAngle = 130 / (180 / Math.PI);
        jd.Initialize(upperArmR,lowerArmR, new Box2D.Common.Math.b2Vec2((startX + 45) / this.m_physScale, 
                                                                    (startY + 20) / this.m_physScale));
        this.m_world.CreateJoint(jd);

        //shoulders to stmatch
        jd.lowerAngle = -15 / (180 / Math.PI);
        jd.upperAngle = 15 / (180 / Math.PI);
        jd.Initialize(torso1,torso2, new Box2D.Common.Math.b2Vec2((startX ) / this.m_physScale, 
                                                                    (startY + 35) / this.m_physScale));
        this.m_world.CreateJoint(jd);
        //stomach to hips;
        jd.Initialize(torso2,torso3, new Box2D.Common.Math.b2Vec2((startX ) / this.m_physScale, 
                                                                    (startY + 50) / this.m_physScale));
        this.m_world.CreateJoint(jd);

        //torso to upper leg
        //l
        jd.lowerAngle = -25 / (180 / Math.PI);
        jd.upperAngle = 45 / (180 / Math.PI);
        jd.Initialize(torso3,upperLegL, new Box2D.Common.Math.b2Vec2((startX - 8) / this.m_physScale, 
                                                                    (startY + 72) / this.m_physScale));
        this.m_world.CreateJoint(jd);
        //r
        jd.lowerAngle = -45 / (180 / Math.PI);
        jd.upperAngle = 25 / (180 / Math.PI);
        jd.Initialize(torso3,upperLegR, new Box2D.Common.Math.b2Vec2((startX + 8) / this.m_physScale, 
                                                                    (startY + 72) / this.m_physScale));
        this.m_world.CreateJoint(jd);

        //upper leg to lower leg
        //l
        jd.lowerAngle = -25 / (180 / Math.PI);
        jd.upperAngle = 115 / (180 / Math.PI);
        jd.Initialize(upperLegL, lowerLegL, new Box2D.Common.Math.b2Vec2((startX - 8) / this.m_physScale, 
                                                                    (startY + 105) / this.m_physScale));
        this.m_world.CreateJoint(jd);
        //r
        jd.lowerAngle = -115 / (180 / Math.PI);
        jd.upperAngle = 25 / (180 / Math.PI);
        jd.Initialize(upperLegR, lowerLegR, new Box2D.Common.Math.b2Vec2((startX + 8) / this.m_physScale, 
                                                                    (startY + 105) / this.m_physScale));
        this.m_world.CreateJoint(jd);
    }
}